package com.all.play.myrecycleviewheadandfooter.view;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

/**
 * Created by 聪明一只哈 on 2018/5/21.
 * Time: 2018/5/21  17:49
 */

public class HeaderViewRecycleViewAdapter extends RecyclerView.Adapter {

    private  RecyclerView.Adapter mAdapter;

    ArrayList<View> mHeaderViewInfos;
    ArrayList<View> mFooterViewInfos;



    public HeaderViewRecycleViewAdapter(
            ArrayList<View> headerViewInfos,
            ArrayList<View> mFooterViewInfo,
            RecyclerView.Adapter mAdapter) {

        this.mAdapter=mAdapter;
        if (headerViewInfos == null) {
            mHeaderViewInfos = new ArrayList<>();
        } else {
            mHeaderViewInfos = headerViewInfos;
        }

        if (mFooterViewInfo == null) {
            mFooterViewInfos = new ArrayList<>();
        } else {
            mFooterViewInfos = mFooterViewInfo;
        }
    }

    @Override
    public int getItemViewType(int position) {
        //判断当前条目是神魔类型

        //----------------------------
        // Header (negative positions will throw an IndexOutOfBoundsException)
        int numHeaders = getHeadersCount();
        if (position < numHeaders) {//返回头部类型
            return RecyclerView.INVALID_TYPE;
        }
        //-------------body------------
        // Adapter
        final int adjPosition = position - numHeaders;
        int adapterCount = 0;
        if (mAdapter != null) {
            adapterCount = mAdapter.getItemCount();
            if (adjPosition < adapterCount) {
                return mAdapter.getItemViewType(adjPosition);
            }
        }

        //---------------footer-----------------

        return RecyclerView.INVALID_TYPE-1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        //header
        if(viewType==RecyclerView.INVALID_TYPE){
            return new HeaderViewHolder(mHeaderViewInfos.get(0));
        }else if (viewType==RecyclerView.INVALID_TYPE-1){
            return new HeaderViewHolder(mFooterViewInfos.get(0));
        }
        // Footer (off-limits positions will throw an IndexOutOfBoundsException
        return mAdapter.onCreateViewHolder(parent,viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        //head
        int numHeaders = getHeadersCount();
        if (position < numHeaders) {//返回头部类型
            return ;
        }

        //body
        final int adjPosition = position - numHeaders;
        int adapterCount = 0;
        if (mAdapter != null) {
            adapterCount = mAdapter.getItemCount();
            if (adjPosition < adapterCount) {
                mAdapter.onBindViewHolder(holder,adjPosition);
                return ;
            }
        }

        //foot



    }

    @Override
    public int getItemCount() {
        if (mAdapter != null) {
            return getFootersCount() + getHeadersCount() + mAdapter.getItemCount();
        } else {
            return getFootersCount() + getHeadersCount();
        }
    }

    public int getFootersCount() {
        return mFooterViewInfos.size();
    }

    public int getHeadersCount() {
        return mHeaderViewInfos.size();
    }


    private static class HeaderViewHolder extends RecyclerView.ViewHolder{

        public HeaderViewHolder(View itemView) {
            super(itemView);
        }
    }
    private static class FooterViewHolder extends RecyclerView.ViewHolder{

        public FooterViewHolder(View itemView) {
            super(itemView);
        }
    }
}
